import 'package:baselib/utils/image_util.dart';
import 'package:baselib/utils/toast_util.dart';
import 'package:flutter/material.dart';
import 'package:framelib/base/base_view_model.dart';
import 'package:framelib/utils/drawable_util.dart';
import 'package:framelib/utils/px_util.dart';
import 'package:framelib/utils/string_util.dart';
import 'package:module_business/module_chat/view/chat_page.dart';
import 'package:module_business/module_chat/view/message_page.dart';
import 'package:module_business/module_comm/service/module_comm_service.dart';
import 'package:get/get.dart';
import 'package:module_business/module_contact/view/contact_page.dart';
import 'package:module_business/module_index/view/index_page.dart';
import 'package:module_business/module_my/view/my_page.dart';

/**
 * 主页逻辑处理层
 */
class HomeViewModel extends BaseViewModel<ModuleCommService> {
  ModuleCommService moduleCommService;
  int last = 0;
  //tabs
  List tabs=[];
  List<Widget> pages=[];
  int tabIndex = 0;

  PageController pageController = PageController();

  @override
  ModuleCommService createService() => (moduleCommService = ModuleCommService());

  //双击退出
  Future<bool> doubleBackExit() {
    int now = DateTime.now().millisecondsSinceEpoch;
    if (now - last > 1000) {
      ToastUtil.toast(StringUtil.instance.Press_again_to_exit);
      last = DateTime.now().millisecondsSinceEpoch;
      return Future.value(false);
    } else {
      return Future.value(true);
    }
  }

  @override
  void onInit() {
    //初始化tab
    initTabs();
    initPages();
    super.onInit();
  }

  void initTabs() {
    tabs=[
      [
        ImagesUtil.loadAssetImage(
            DrawableUtil.instance.icon_selected_index,
            width: PxUtil.instance.px_30,
            height: PxUtil.instance.px_30),
        ImagesUtil.loadAssetImage(
          DrawableUtil.instance.icon_unselected_index,
          width: PxUtil.instance.px_30,
          height: PxUtil.instance.px_30,
        )
      ],
      [
        ImagesUtil.loadAssetImage(
            DrawableUtil.instance.icon_selected_contact,
            width: PxUtil.instance.px_30,
            height: PxUtil.instance.px_30),
        ImagesUtil.loadAssetImage(
          DrawableUtil.instance.icon_unselected_contact,
          width: PxUtil.instance.px_30,
          height: PxUtil.instance.px_30,
        )
      ],
      [
        ImagesUtil.loadAssetImage(
            DrawableUtil.instance.icon_selected_chat,
            width: PxUtil.instance.px_30,
            height: PxUtil.instance.px_30),
        ImagesUtil.loadAssetImage(
          DrawableUtil.instance.icon_unselected_chat,
          width: PxUtil.instance.px_30,
          height: PxUtil.instance.px_30,
        )
      ],
      [
        ImagesUtil.loadAssetImage(
            DrawableUtil.instance.icon_selected_my,
            width: PxUtil.instance.px_30,
            height: PxUtil.instance.px_30),
        ImagesUtil.loadAssetImage(
          DrawableUtil.instance.icon_unselected_my,
          width: PxUtil.instance.px_30,
          height: PxUtil.instance.px_30,
        )
      ],
    ];
  }


  getTabIcon(int curIndex) {
    if (curIndex == tabIndex) {
      return tabs[curIndex][0];
    }
    return tabs[curIndex][1];
  }

  onBottomItemTap(int index) {
    tabIndex=index;
    update();
    pageController.jumpToPage(tabIndex);
  }

  onPageChanged(int pageIndex) {
    tabIndex=pageIndex;
    update();
  }

  void initPages() {
    pages=[
      IndexPage(),
      ContactPage(),
      MessagePage(),
      MyPage(),
    ];
  }
}
